package com.lishem.rpc.client.cluster.impl;

import com.lishem.rpc.client.channel.ProviderService;
import com.lishem.rpc.client.cluster.ClusterStrategy;
import org.apache.commons.lang3.RandomUtils;

import java.util.List;

/**
 * 随机策略
 */
public class RandomClusterStrategyImpl implements ClusterStrategy {

    @Override
    public ProviderService select(List<ProviderService> serviceRoutes) {
        // 获取可用的服务总数
        int MAX_LEN = serviceRoutes.size();
        // 通过随机工具获取服务编号
        int index = RandomUtils.nextInt(0, MAX_LEN - 1);
        return serviceRoutes.get(index);
    }
}
